package cmp.viewer.panels;

import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import cmp.main.Globals;

public class EditMethodPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private static final int MODE_EDIT = 0;
	private static final int MODE_ADD = 1;

	public EditMethodPanel() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		JLabel lblName = new JLabel("Name");
		JLabel lblFile = new JLabel("File");
		JLabel lblComment = new JLabel("Comment");
		
		lblName.setPreferredSize(lblComment.getPreferredSize());
		lblFile.setPreferredSize(lblComment.getPreferredSize());
		
		JPanel namePanel = new JPanel();
		namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.LINE_AXIS));
		namePanel.setBackground(Globals.BGCOLOR);
		namePanel.add(lblName);
		txtName = new JTextField(30);
		namePanel.add(Box.createHorizontalStrut(5));
		namePanel.add(txtName);
		
		JPanel filePanel = new JPanel();
		filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.LINE_AXIS));
		filePanel.setBackground(Globals.BGCOLOR);
		filePanel.add(lblFile);
		txtFile = new JTextField(30);
		filePanel.add(Box.createHorizontalStrut(5));
		filePanel.add(txtFile);

		JPanel commentPanel = new JPanel();
		commentPanel.setLayout(new BoxLayout(commentPanel, BoxLayout.LINE_AXIS));
		commentPanel.setBackground(Globals.BGCOLOR);
		commentPanel.add(lblComment);
		txtComment = new JTextField(30);
		commentPanel.add(Box.createHorizontalStrut(5));
		commentPanel.add(txtComment);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
		buttonPanel.setBackground(Globals.BGCOLOR);
		btnOK = new JButton("Save");
		btnOK.setBackground(Globals.BGCOLOR);
		btnCancel = new JButton("Discard");
		btnCancel.setBackground(Globals.BGCOLOR);
		buttonPanel.add(btnOK);
		buttonPanel.add(Box.createHorizontalStrut(10));
		buttonPanel.add(btnCancel);
		
		add(Box.createVerticalStrut(15));
		add(namePanel);
		add(Box.createVerticalStrut(5));
		add(filePanel);
		add(Box.createVerticalStrut(5));
		add(commentPanel);
		add(Box.createVerticalStrut(15));
		add(buttonPanel);
		
		CaretListener cl = new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				updateOKButton();
			}
		};
		txtName.addCaretListener(cl);
		txtFile.addCaretListener(cl);
		
		setMaximumSize(getPreferredSize());
		updateOKButton();
	}
	
	public void setMode(int mode) {
		nMode = mode;
		if(mode == MODE_EDIT)
			btnOK.setText("Save");
		else if(mode == MODE_ADD)
			btnOK.setText("Add");
	}
	
	public int getMode() { return nMode; }
	
	public void setValues(String name, String file, String comment) {
		txtName.setText(name);
		txtFile.setText(file);
		txtComment.setText(comment);
		updateOKButton();
	}
	
	public String getName() { return txtName.getText(); }
	public String getFile() { return txtFile.getText(); }
	public String getComment() { return txtComment.getText(); }
	
	public void addOKListener(ActionListener l) {
		btnOK.addActionListener(l);
	}
	
	public void addCancelListener(ActionListener l) {
		btnCancel.addActionListener(l);
	}
	
	private void updateOKButton() {
		if(txtName.getText().trim().length() > 0 && txtFile.getText().trim().length() > 0)
			btnOK.setEnabled(true);
		else
			btnOK.setEnabled(false);
	}
	
	private JTextField txtName = null, txtFile = null, txtComment = null;
	private JButton btnOK = null, btnCancel = null; 
	private int nMode = -1;
}
